<?php

namespace App\Http\Controllers;

use App\Models\Address;
use App\Models\Area;
use App\Models\AutoReply;
use App\Models\BrandSort;
use App\Models\CarouselPicCategory;
use App\Models\ClinicCategory;
use App\Models\ConsultSetting;
use App\Models\CourseCategory;
use App\Models\Culture;
use App\Models\Doctor;
use App\Models\FinancialStatementsLog;
use App\Models\Footprint;
use App\Models\HelpAnswer;
use App\Models\HelpCates;
use App\Models\MakeMoney;
use App\Models\PlanTime;
use App\Models\ProductSort;
use App\Models\Recruit;
use App\Models\ReplyContent;
use App\Models\Supplier;
use App\Models\SystemMessage;
use App\Models\WorkRoomSortCategory;
use App\User;
use App\Util\Tool;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;
use Illuminate\Support\Facades\DB;

class HomeController extends BaseController
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
//        $this->middleware('auth');
    }

    public function getChild(Request $request)
    {
        $pid = $request->get("q");
        $options = ClinicCategory::where([['pid','=',$pid]])->select('id', 'classify_name as text')->get();
        $selectOption = [];
        foreach ($options as $option) {
            $selectOption[$option->id] = $option->text;
        }
        return $selectOption;
    }




    public function getBill(Request $request)
    {
        $param = $request->input();
        if(isset($param['type']) && $param['type'] == 1){

        }elseif (isset($param['type']) && $param['type'] == 2){

        }

        return view('home');
    }

    public function download()
    {
        return view("download");
    }


    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        echo "工作室ID:".$request->input("work_id");
        return view('home');
    }


    public function getAreasList()
    {
        $result = (new Area())->getAreasList();
        return $this->ResultJson(1, '获取成功!', $result);
    }
    
    

    /***
     * @param Request $request
     * @return mixed
     * 获取轮播图
     */
    public function getTopImgs(Request $request)
    {
        $param = $request->input();
        if (empty($param['cate_id'])) {
            $param['cate_id'] = 1;
        }
        $imgs = (new CarouselPicCategory())->getImages($param);
        return $this->ResultJson(1, '获取成功!', $imgs);
    }


    /***
     * @param Request $request
     * @return mixed
     * 获取课程分类
     */
    public function getCourseCategoryList(Request $request)
    {
        $param = $request->input();
        $imgs = (new CourseCategory())->getCourseCategoryList($param);
        return $this->ResultJson(1, '获取成功!', $imgs);
    }

    /***
     * @param Request $request
     * @return mixed
     * 工作室和问诊分类
     */
    public function getWorksCategoryList(Request $request)
    {
        $param = $request->input();
        $imgs = (new ClinicCategory())->getWorksCategoryList($param);
        return $this->ResultJson(1, '获取成功!', $imgs);
    }

    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 标签分类
     */
    public function getTargetCategoryList(Request $request)
    {
        $param = $request->input();
        $imgs = (new ClinicCategory())->getTargetCategoryList($param);
        return $this->ResultJson(1, '获取成功!', $imgs);
    }


     /***
     * @param Request $request
     * @return mixed
     * 商品分类
     */
    public function getGoodsCategoryList(Request $request)
    {
        $param = $request->input();
        $imgs = (new ProductSort())->getGoodsCategoryList($param);
        return $this->ResultJson(1, '获取成功!', $imgs);
    }

  /***
     * @param Request $request
     * @return mixed
     *工作室自己的分类
     */
    public function getWorkSelfCategoryList(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, '工作室id不能为空!', []);
        }
        $imgs = (new WorkRoomSortCategory())->getWorkSelfCategoryList($param);
        return $this->ResultJson(1, '获取成功!', $imgs);
    }

    /***
     * @param Request $request
     * 充值
     */
    public function recharge(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户id不能为空!', []);
        }
        if (empty($param['money'])) {
            return $this->ResultJson(0, '金额不能为空!', []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, '充值类型不能为空!', []);
        }
        if ($param['money'] <= 0 || !is_numeric($param['money'])) {
            return $this->ResultJson(0, '金额不能小于0!', []);
        }
        $imgs = (new MakeMoney())->recharge($param);
        return $this->ResultJson(1, '获取成功!', $imgs);
        
    }

    

    /***
     * @param Request $request
     * @return mixed
     * 添加足迹
     */
    public function addFootPrint(Request $request)
    {
        $param = $request->input();
        if (empty($param['goods_id'])) {
            return $this->ResultJson(0, '商品ID不能为空id!', []);
        }
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户id不能为空id!', []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, '类型不能为空id!', []);
        }
        $res = (new Footprint())->addFootPrint($param);
        if ($res) {
            return $this->ResultJson(1, '添加成功!', []);
        }
    }


    /***
     * @param Request $request
     * @return mixed
     * 获取足迹
     */
    public function getFootPrint(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户id不能为空id!', []);
        }
        $res = (new Footprint())->getFootPrint($param);
        if ($res) {
            return $this->ResultJson(1, '获取成功!', $res);
        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * IOS
     */
    public function getFootPrintIos(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户id不能为空id!', []);
        }
        $res = (new Footprint())->getFootPrintIos($param);
        if ($res) {
            return $this->ResultJson(1, '获取成功!', $res);
        }
    }

    /***
     * @param Request $request
     * @return mixed
     * 添加收货地址
     */
    public function addAddress(Request $request)
    {
        $param = $request->input();
        if (empty($param['name'])) {
            return $this->ResultJson(0, '名称不能为空id!', []);
        }
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户id不能为空id!', []);
        }
        if (empty($param['phone'])) {
            return $this->ResultJson(0, '电话不能为空id!', []);
        }
        if (!$this->isValidPhoneNumber($param['phone'])) {
            return $this->ResultJson(0, '电话格式不正确!', []);
        }
        if (empty($param['province_id'])) {
            return $this->ResultJson(0, '省id不能为空id!', []);
        }
        if (empty($param['city_id'])) {
            return $this->ResultJson(0, '市id不能为空id!', []);
        }
        if (empty($param['county_id'])) {
            return $this->ResultJson(0, '县id不能为空id!', []);
        }
        if (empty($param['detail'])) {
            return $this->ResultJson(0, '详细地址不能为空id!', []);
        }
        $res = (new Address())->addAddress($param);
        if ($res) {
            if (isset($param['id'])) {
                return $this->ResultJson(1, '编辑成功!', []);
            }
            return $this->ResultJson(1, '添加成功!', []);
        }
    }

    /***
     * @param $number
     * @return false|int
     * 验证手机号
     */
    protected function isValidPhoneNumber($number) {
        return preg_match('/^1[3-9]\d{9}$/', $number);
    }


    /***
     * @param Request $request
     * @return mixed
     * 获取收货地址
     */
    public function getAddressList(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户id不能为空!', []);
        }
        $res = (new Address())->getAddressList($param);
        if ($res) {
            return $this->ResultJson(1, '获取成功!', $res);
        }else{
            return $this->ResultJson(0, '暂无数据!', []);
        }
    }


    /***
     * @param Request $request
     * @return mixed
     * 获取收货地址详情
     */
    public function getAddressDetail(Request $request)
    {
        $param = $request->input();
        if (empty($param['id'])) {
            return $this->ResultJson(0, '地址id不能为空!', []);
        }
        $res = (new Address())->getAddressInfo($param['id']);
        if ($res) {
            return $this->ResultJson(1, '获取成功!', $res);
        } else {
            return $this->ResultJson(0, '暂无数据!', []);
        }
    }



    /***
     * @param Request $request
     * @return mixed
     * 删除收货地址
     */
    public function delAddress(Request $request)
    {
        $param = $request->input();
        if (empty($param['id'])) {
            return $this->ResultJson(0, '收货id不能为空!', []);
        }
        $res = (new Address())->delAddress($param);
        if ($res) {
            return $this->ResultJson(1, '删除成功!', []);
        }else{
            return $this->ResultJson(0, '删除失败!', []);
        }
    }

    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 帮助分类
     */
    public function getHelpCates(Request $request)
    {
        $param = $request->input();
        $res = (new HelpCates())->getHelpCates($param);
        if ($res) {
            return $this->ResultJson(1, '获取成功!', $res);
        }else{
            return $this->ResultJson(0, '暂无数据!', []);
        }
    }


    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 通过分类查问题
     */
    public function getHelpCatesAnswer(Request $request)
    {
        $param = $request->input();
        $res = (new HelpAnswer())->getHelpAnswer($param);
        if ($res) {
            return $this->ResultJson(1, '获取成功!', $res);
        }else{
            return $this->ResultJson(0, '暂无数据!', []);
        }
    }


    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 问题详情
     */
    public function getHelpCatesAnswerDetail(Request $request)
    {
        $param = $request->input();
        $res = (new HelpAnswer())->getHelpAnswerDetail($param);
        if ($res) {
            return $this->ResultJson(1, '获取成功!', $res);
        }else{
            return $this->ResultJson(0, '暂无数据!', []);
        }
    }

    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 问题详情
     */
    public function getCulturesDetail(Request $request)
    {
        $param = $request->input();
        $res = (new Culture())->getCulturesDetail($param);
        if ($res) {
            return $this->ResultJson(1, '获取成功!', $res);
        }else{
            return $this->ResultJson(0, '暂无数据!', []);
        }
    }

    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 申请供货商
     */
    public function addSuppliers(Request $request)
    {
        $res = $request->input() ?: [];
        $param = $this->verify($res);
        if ($param === true) {
            $result = (new Supplier())->addSuppliers($res);
            if (is_array($result)) {
                return $this->ResultJson(1, '提交成功!', $result);
            } else {
                return $this->ResultJson(0, $result, []);
            }
        }
        return $this->ResultJson(0, $param, []);;
    }


    /****
     * @param $res
     * @return bool|\Illuminate\Http\JsonResponse
     * 数据验证
     */
    public function verify($res)
    {
        if (empty($res['user_id'])) {
            return '用户id不能为空!';
        }
        if (empty($res['phone']) || !isset($res['phone'])) {
            return '手机号不能为空!';
        }
        if (!$this->isValidPhoneNumber($res['phone'])) {
            return '电话格式不正确!';
        }
        if (empty($res['account'])) {
            return '账号不能为空!';
        }
        if (empty($res['pass'])) {
            return '密码不能为空!';
        }
        if (empty($res['confirm_pass'])) {
            return '确认密码不能为空!';
        }
        if($res['pass'] != $res['confirm_pass']){
            return '两次输入密码不相同!';
        }
        if (empty($res['user_name'])) {
            return '姓名不能为空!';
        }
        if (empty($res['front_card'])) {
            return '身份证正面不能为空!';
        }
        if (empty($res['behind_card'])) {
            return '身份证反面不能为空!';
        }
        if (empty($res['company'])) {
            return '企业名称不能为空!';
        }
        if (empty($res['license_img'])) {
            return '营业执照不能为空!';
        }
        return true;
    }


    /**
     * 
     */
    public function getAddressByPid(Request $request)
    {
        $res = $request->input();
        $result = (new Area())->getAddressByPid($res);
        return $this->ResultJson(1, "获取成功!",$result);;
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 招募广告
     */
    public function recruitNotice(Request $request)
    {
        $res = new Recruit();
        $result = $res::getRecruitNotice();
        return $this->ResultJson(1, "获取成功!",$result);;
    }
    
    /**
     * 
     * 排单
     */
    public function setDoctorTime(Request $request)
    {
        $res = $request->input();
        if (empty($res['dates'])) {
            return $this->ResultJson(0, "日期不能为空!", []);
        }
        if(strtotime($res['dates']) < strtotime(date("Y-m-d",time()))){
            return $this->ResultJson(0, "该日期已过,不能排单!", []);
        }
//        if (empty($res['times'])) {
//            return $this->ResultJson(0, "时间不能为空!", []);
//        }
        if (empty($res['work_id'])) {
            return $this->ResultJson(0, "工作室ID不能为空!", []);
        }
        if (empty($res['doctor_id'])) {
            return $this->ResultJson(0, "医生ID不能为空!", []);
        }
        if (empty($res['type'])) {
            return $this->ResultJson(0, "类型不能为空!", []);
        }
        $res = (new PlanTime())->add($res);
        if ($res === true) {
            return $this->ResultJson(1, "排单成功!", []);
        }
        return $this->ResultJson(0, "排单失败!", []);
    }

    /**
     *
     * 获取排单列表
     */
    public function getDoctorTimeList(Request $request)
    {
        $res = $request->input();
        if (empty($res['dates'])) {
            return $this->ResultJson(0, "日期不能为空!", []);
        }
        if (empty($res['work_id'])) {
            return $this->ResultJson(0, "工作室ID不能为空!", []);
        }
        if (empty($res['doctor_id'])) {
            return $this->ResultJson(0, "医生ID不能为空!", []);
        }
        if (empty($res['type'])) {
            return $this->ResultJson(0, "类型不能为空!", []);
        }
        $res = (new PlanTime())->getList($res);
        if ($res) {
            return $this->ResultJson(1, "获取成功!", $res);
        }
        return $this->ResultJson(1, "暂无数据!", []);
    }

    /**
     *
     * 添加自动回复
     */
    public function addAutoReply(Request $request)
    {
        $res = $request->input();
        if (empty($res['type'])) {
            return $this->ResultJson(0, "类型不能为空!", []);
        }
        if (empty($res['work_id'])) {
            return $this->ResultJson(0, "工作室ID不能为空!", []);
        }
        if (empty($res['content'])) {
            return $this->ResultJson(0, "回复内容不能为空!", []);
        }
        $result = (new AutoReply())->add($res);
        if ($result) {
            if (isset($res['id'])) {
                return $this->ResultJson(1, "编辑成功!", []);
            }
            return $this->ResultJson(1, "添加成功!", []);
        }
        return $this->ResultJson(0, "添加失败!", []);
    }

    /**
     *
     * 获取自动回复列表
     */
    public function getAutoReplyList(Request $request)
    {
        $res = $request->input();
        if (empty($res['work_id'])) {
            return $this->ResultJson(0, "工作室ID不能为空!", []);
        }
        $result = (new AutoReply())->getList($res);
        if ($result) {
            return $this->ResultJson(1, "获取成功!", $result);
        }
        return $this->ResultJson(0, "暂无数据!", []);
    }
    /**
     *
     * 删除自动回复
     */
    public function delAutoReply(Request $request)
    {
        $res = $request->input();
        if (empty($res['id'])) {
            return $this->ResultJson(0, "id不能为空!", []);
        }
        $result = (new AutoReply())->del($res);
        if ($result) {
            return $this->ResultJson(1, "删除成功!", []);
        }
        return $this->ResultJson(0, "添加失败!", []);
    }


    /**
     *
     * 添加回复列表
     */
    public function addReplyContent(Request $request)
    {
        $res = $request->input();
        if (empty($res['doctor_id'])) {
            return $this->ResultJson(0, "医师ID不能为空!", []);
        }
        if (empty($res['evaluates_id'])) {
            return $this->ResultJson(0, "评价ID不能为空!", []);
        }
        if (empty($res['content'])) {
            return $this->ResultJson(0, "回复内容不能为空!", []);
        }
        $result = (new ReplyContent())->add($res);
        if ($result ===true ||  $result ===1) {
            if (isset($res['id'])) {
                return $this->ResultJson(1, "编辑成功!", []);
            }
            return $this->ResultJson(1, "回复成功!", []);
        }
        return $this->ResultJson(0, $result, []);
    }

    /**
     *
     * 获取系统系统消息
     */
    public function getSystemMessage(Request $request)
    {
        $param = $request->input();
        if (empty($param['type'])) {
            return $this->ResultJson(0, "类型不能为空!", []);
        }
        $list = (new SystemMessage())->getList($param);
        return $this->ResultJson(1, "获取成功!", $list);
    }

   /**
     *
     * 获取系统系统消息
     */
    public function getSystemMessageDetail(Request $request)
    {
        $res = $request->input();
        if (empty($res['id'])) {
            return $this->ResultJson(0, "系统消息id不能为空!", []);
        }
        $list = (new SystemMessage())->getListDetail($res);
        return $this->ResultJson(1, "获取成功!", $list);
    }

    /**
     *
     * 在线咨询设置
     */
    public function onlineConsultSetting(Request $request)
    {
        $res = $request->input();
        if (empty($res['work_id'])) {
            return $this->ResultJson(0, "工作室id不能为空!", []);
        }
        if (empty($res['doctor_id'])) {
            return $this->ResultJson(0, "医生id不能为空!", []);
        }
        if (empty($res['type'])) {
            return $this->ResultJson(0, "类型不能为空!", []);
        }
        $result = (new ConsultSetting())->add($res);
        if($result){
            return $this->ResultJson(1, "设置成功!", []);
        }
        return $this->ResultJson(0, "设置失败!", []);
    }


    /**
     * 获取在线咨询设置
     *
     */
    public function getOnlineConsultDetail(Request $request)
    {
        $res = $request->input();
        if (empty($res['work_id'])) {
            return $this->ResultJson(0, "工作室id不能为空!", []);
        }
        if (empty($res['doctor_id'])) {
            return $this->ResultJson(0, "医生id不能为空!", []);
        }
//        if (empty($res['type'])) {
//            return $this->ResultJson(0, "类型不能为空!", []);
//        }
        $result = (new ConsultSetting())->getDetailNoType($res);
        if(!empty($result)){
            return $this->ResultJson(1, "获取成功!", $result->toArray());
        }
        return $this->ResultJson(0, "暂无数据!",[]);


    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取总账单
     *
     */
    public function getAllTotal(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室id不能为空!", []);
        }
        $result = (new FinancialStatementsLog())->getAllTotal($param);
        if($result){
            return $this->ResultJson(1, "获取成功!", $result);
        }
        return $this->ResultJson(0, "获取失败!", []);
    }

    /**
     * @param Request $request
     * 获取echart数据
     */
    public function getEchartMapData(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室id不能为空!", []);
        }
        $res = (new FinancialStatementsLog())->getEchartMapData($param);
        return $this->ResultJson(1, "获取成功!", $res);
    }
    
    
    

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取账单列表
     */
    public function getAllStatementList(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室id不能为空!", []);
        }
        $result = (new FinancialStatementsLog())->getAllStatementList($param);
        if ($result) {
            return $this->ResultJson(1, "获取成功!", $result);
        }
        return $this->ResultJson(0, "获取失败!", []);

    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 分类列表
     */
    public function getBrandList(Request $request)
    {
        $param = $request->input();
        if (empty($param['pid'])) {
            $param['pid'] = 0;
        }
        $res = (new BrandSort())->where([['pid', '=', $param['pid']]])->get();
        if (!empty($res)) {
            $result = $res->toArray();
            return $this->ResultJson(1, "获取成功!", $result);
        }
        return $this->ResultJson(0, "暂无数据!", []);
    }


    public function getEasemobToken()
    {
        $token = (new Easemob())->getToken();
        return $this->ResultJson(1, "获取成功!", ["token" => $token]);
    }
    

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 环信注册单个用户
     */
    public function createUser(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, "用户ID不能为空!", []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, "人员类型不能为空!", []);
        }
        $tool = Tool::getUserNameForIm($param);
        $res = (new Easemob())->createUser($tool['username'], $tool['password']);
        if(!isset($res['error'])){
            return $this->ResultJson(1, "注册成功!", $res);
        }
        return $this->ResultJson(0, "注册失败!", []);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 环信获取单个用户
     */
    public function getUser(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, "用户ID不能为空!", []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, "人员类型不能为空!", []);
        }
        $tool = Tool::getUserNameForIm($param);
        $res = (new Easemob())->getUser($tool['username']);
        if(!isset($res['error'])){
            return $this->ResultJson(1, "获取成功!", $res);
        }
        return $this->ResultJson(0, "获取失败!", []);
    }

     /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 环信判断用户是否在线
     */
    public function isOnline(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, "用户ID不能为空!", []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, "人员类型不能为空!", []);
        }
        $tool = Tool::getUserNameForIm($param);
        $res = (new Easemob())->isOnline($tool['username']);
        if(!isset($res['error'])){
            return $this->ResultJson(1, "获取成功!", $res);
        }
        return $this->ResultJson(0, "获取失败!", []);
    }


    /***
     * 强制线下
     * 
     */

    public function outLogin(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, "用户ID不能为空!", []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, "人员类型不能为空!", []);
        }
        $tool = Tool::getUserNameForIm($param);
        $res = (new Easemob())->outLogin($tool);
        if(!isset($res['error'])){
            return $this->ResultJson(1, "下线成功!", []);
        }
        return $this->ResultJson(0, "获取失败!", []);

        
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 环信查看用户离线消息数
     */
    public function getOfflineMessages(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, "用户ID不能为空!", []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, "人员类型不能为空!", []);
        }
        $tool = Tool::getUserNameForIm($param);
        $res = (new Easemob())->getOfflineMessages($tool['username']);
        if (!isset($res['error'])) {
            return $this->ResultJson(1, "获取成功!", $res);
        }
        return $this->ResultJson(0, "获取失败!", []);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 环信发送文本消息
     */
    public function sendTextBySdk(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, "用户ID不能为空!", []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, "人员类型不能为空!", []);
        }
        if (empty($param['text'])) {
            return $this->ResultJson(0, "消息不能为空!", []);
        }
        $tool = Tool::getUserNameForIm($param);
        $param['username'] = $tool['username'];
        $res = (new Easemob())->sendTextBySdk($param);
        if (!isset($res['error'])) {
            return $this->ResultJson(1, "发送成功!", $res);
        }
        return $this->ResultJson(0, "发送失败!", []);
    }


    public function sendTextBySdkTest()
    {
        $param['username'] = '15801512319';
        $param['text'] = '15801512319';
        $res = (new Easemob())->sendTextBySdk($param);
        dd($res);
    }

    /**
     * admin账号发送群聊
     *
     */
    public function sendGroupText()
    {
        $res = (new Easemob())->sendGroupText("admin","247212103761924","惊天修改");
        dd($res);
    }
    
    
    
    
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 环信发送图文消息
     */
    public function sendImageBySdk(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, "用户ID不能为空!", []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, "人员类型不能为空!", []);
        }
        if (empty($param['file_uuid'])) {
            return $this->ResultJson(0, "文件UU_ID不能为空!", []);
        }
        $tool = Tool::getUserNameForIm($param);
        $param['username'] = $tool['username'];
        $res = (new Easemob())->sendImageBySdk($param);
        if (!isset($res['error'])) {
            return $this->ResultJson(1, "发送成功!", $res);
        }
        return $this->ResultJson(0, "发送失败!", []);
    }
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 环信发送语音消息
     */
    public function sendAudioBySdk(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, "用户ID不能为空!", []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, "人员类型不能为空!", []);
        }
        if (empty($param['file_uuid'])) {
            return $this->ResultJson(0, "文件UU_ID不能为空!", []);
        }
        $tool = Tool::getUserNameForIm($param);
        $param['username'] = $tool['username'];
        $res = (new Easemob())->sendAudioBySdk($param);
        if (!isset($res['error'])) {
            return $this->ResultJson(1, "发送成功!", $res);
        }
        return $this->ResultJson(0, "发送失败!", []);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 环信发送视频消息
     */
    public function sendVideoBySdk(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, "用户ID不能为空!", []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, "人员类型不能为空!", []);
        }
        if (empty($param['file_uuid'])) {
            return $this->ResultJson(0, "文件UU_ID不能为空!", []);
        }
        $tool = Tool::getUserNameForIm($param);
        $param['username'] = $tool['username'];
        $res = (new Easemob())->sendVideoBySdk($param);
        if (!isset($res['error'])) {
            return $this->ResultJson(1, "发送成功!", $res);
        }
        return $this->ResultJson(0, "发送失败!", []);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 环信发送语音消息
     */
    public function sendFileBySdk(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, "用户ID不能为空!", []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, "人员类型不能为空!", []);
        }
        if (empty($param['file_uuid'])) {
            return $this->ResultJson(0, "文件UU_ID不能为空!", []);
        }
        $tool = Tool::getUserNameForIm($param);
        $param['username'] = $tool['username'];
        $res = (new Easemob())->sendFileBySdk($param);
        if (!isset($res['error'])) {
            return $this->ResultJson(1, "发送成功!", $res);
        }
        return $this->ResultJson(0, "发送失败!", []);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 环信获取历史消息
     */
    public function getHistoryAsUri(Request $request)
    {
        $param = $request->input();
        if (empty($param['time'])) {
            return $this->ResultJson(0, "时间不能为空!", []);
        }
        $res = (new Easemob())->getHistoryAsUri($param);
        if (!isset($res['error'])) {
            return $this->ResultJson(1, "发送成功!", $res);
        }
        return $this->ResultJson(0, "发送失败!", []);
    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 同步推送
     */
    public function sendSyncPush(Request $request)
    {
        $param = $request->input();
        if (empty($param['user'])) {
            return $this->ResultJson(0, "时间不能为空!", []);
        }
        if (empty($param['title'])) {
            return $this->ResultJson(0, "标题不能为空!", []);
        }
        if (empty($param['content'])) {
            return $this->ResultJson(0, "内容不能为空!", []);
        }
        $res = (new Easemob())->sendSyncPush($param);
        if (!isset($res['error'])) {
            return $this->ResultJson(1, "发送成功!", $res);
        }
        return $this->ResultJson(0, "发送失败!", []);
    }

    public function sendSinglePush(Request $request)
    {
        $param = $request->input();
        if (empty($param['user'])) {
            return $this->ResultJson(0, "人员列表不能为空!", []);
        }
        if (empty($param['title'])) {
            return $this->ResultJson(0, "标题不能为空!", []);
        }
        if (empty($param['sub_title'])) {
            return $this->ResultJson(0, "小标题标题不能为空!", []);
        }
        if (empty($param['content'])) {
            return $this->ResultJson(0, "内容不能为空!", []);
        }
        $res = (new Easemob())->sendSinglePush($param);
        if (!isset($res['error'])) {
            return $this->ResultJson(1, "发送成功!", $res);
        }
        return $this->ResultJson(0, "发送失败!", []);
    }



    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 环信上传文件
     */
    public function uploadFileBySdk(Request $request)
    {
        $param = $request->input();
//        if (empty($param['user_id'])) {
//            return $this->ResultJson(0, "用户ID不能为空!", []);
//        }
        if (empty($param['file'])) {
            return $this->ResultJson(0, "文件地址不能为空!", []);
        }
//        if (empty($param['text'])) {
//            return $this->ResultJson(0, "消息不能为空!", []);
//        }
//        $tool = Tool::getUserNameForIm($param);
//        $param['username'] = $tool['username'];
        $res = (new Easemob())->uploadFileBySdk($param);
        if (!isset($res['error'])) {
            return $this->ResultJson(1, "上传成功!", $res);
        }
        return $this->ResultJson(0, "上传失败!", []);
    }


    public function isUsersOnline(Request $request)
    {
        $res = (new Easemob())->isUsersOnline();
        if (!isset($res['error'])) {
            return $this->ResultJson(1, "成功!", []);
        }
        return $this->ResultJson(0, "失败!", []);
    }




    /**
     * 创建群主
     */
    public function makeChatRoom(Request $request)
    {
        $param = $request->input();
        if (empty($param['admin'])) {
            return $this->ResultJson(0, "创群人员不能为空!", []);
        }
        if (empty($param['group_name'])) {
            return $this->ResultJson(0, "标题不能为空!", []);
        }
        if (empty($param['private_group_desc'])) {
            return $this->ResultJson(0, "小标题标题不能为空!", []);
        }
        if (empty($param['user'])) {
            return $this->ResultJson(0, "内容不能为空!", []);
        }
        if (empty($param['order_id'])) {
            return $this->ResultJson(0, "订单ID不能为空!", []);
        }
        $result = (new Easemob())->makeChatRoom($param);
        if($result === true){
            return $this->ResultJson(1, "创建群成功!", []);
        }
        return $this->ResultJson(0, "创建群失败!", []);
    }


    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 解散群
     */
    public function dissolveChatRoom(Request $request)
    {
        $param = $request->input();
        if (empty($param['order_id'])) {
            return $this->ResultJson(0, "订单ID不能为空!", []);
        }
        $result = (new Easemob())->dissolveChatRoom($param);
        if($result === true){
            return $this->ResultJson(1, "解散群成功!", []);
        }
        return $this->ResultJson(0, "解散群失败!", []);
    }


    /***
     *
     * 获取平台客服IM
     */
    public function getPlatformIm()
    {
        $data['im_num'] = config("omnipay.PlatformIm");
        return $this->ResultJson(1, "获取成功!", $data);
    }

    /**
     * 获取推送token
     */
    public function setUserToken(Request $request)
    {
        $param = $request->input();
        if (empty($param['type'])) {
            return $this->ResultJson(0, "用户类型不能为空!", []);
        }
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, "用户ID不能为空!", []);
        }
        if (empty($param['device_type'])) {
            return $this->ResultJson(0, "设备类型不能为空!", []);
        }
        if (empty($param['tpns_token'])) {
            return $this->ResultJson(0, "token不能为空!", []);
        }
        if ($param['type'] == 1) { //用户
            $user = (new User())->where([['id', '=', $param['user_id']]])->update(['use_phone_type' => $param['device_type'], 'tpns_token' => $param['tpns_token']]);
        } else { //医师
            $user = (new Doctor())->where([['id', '=', $param['user_id']]])->update(['use_phone_type' => $param['device_type'], 'tpns_token' => $param['tpns_token']]);
        }
        if ($user) {
            return $this->ResultJson(1, "更新成功!", []);
        }
        return $this->ResultJson(0, "更新失败!", []);
    }




}
